package dept

import (
	"context"
	"go-zero-project/services/user/rpc/pb"

	"go-zero-project/services/user/api/internal/svc"
	"go-zero-project/services/user/api/internal/types"

	"github.com/zeromicro/go-zero/core/logx"
)

type SaveLogic struct {
	logx.Logger
	ctx    context.Context
	svcCtx *svc.ServiceContext
}

func NewDeptSaveLogic(ctx context.Context, svcCtx *svc.ServiceContext) *SaveLogic {
	return &SaveLogic{
		Logger: logx.WithContext(ctx),
		ctx:    ctx,
		svcCtx: svcCtx,
	}
}

func (l *SaveLogic) DeptSave(req *types.DeptSaveReq) (resp *types.DeptSaveResp, err error) {
	// 1.builder rpc request
	saveReq := pb.DeptSaveReq{Name: req.Name, ParentId: req.ParentId, OrderNum: int32(req.OrderNum)}
	// 2.request rpc
	deptSave, err := l.svcCtx.UserCenterClient.DeptSave(l.ctx, &saveReq)
	if err != nil {
		return nil, err
	}
	// 3.return obj
	return &types.DeptSaveResp{
		DeptId: deptSave.DeptId,
	}, nil
}
